<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : !disabled ? '修改' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" inline label-width="120px">
      <el-form-item label="分类" prop="categoryId">
        <el-select v-model="dataForm.categoryId" :disabled="disabled" clearable filterable placeholder="请选择"
                   class="width185">
          <el-option
            v-for="item in categoryList"
            :key="item.id"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序字段" prop="sort">
        <el-input v-model="dataForm.sort" :disabled="disabled" placeholder="排序字段"></el-input>
      </el-form-item>
      <el-form-item label="商品" prop="goodsId">
        <el-select v-model="dataForm.goodsId" :disabled="disabled" clearable filterable remote placeholder="请选择"
                   :remote-method="remoteMethod"
                   class="width185">
          <el-option
            v-for="item in goodsList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" :disabled="disabled" placeholder="标题"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
<!--        <el-input v-model="dataForm.status" :disabled="disabled" placeholder="类型 1=上架 2=下架"></el-input>-->
        <el-radio-group v-model="dataForm.status">
          <el-radio :label=1>上架</el-radio>
          <el-radio :label=2>下架</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="dataForm.goodsId==''|| dataForm.goodsId==null" label="跳转地址,与商品id互斥" prop="link">
        <el-input v-model="dataForm.link" :disabled="disabled" placeholder="跳转地址"></el-input>
      </el-form-item>
      <el-form-item label="封面" prop="cover">
        <el-img v-model="dataForm.cover" :disabled="disabled">
        </el-img>
        <el-input v-model="dataForm.cover" :disabled="disabled" placeholder="封面"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="dataForm.remarks" :disabled="disabled" placeholder="备注"></el-input>
      </el-form-item>
<!--      <el-form-item label="首页展现" prop="homepageShow">-->
<!--&lt;!&ndash;        <el-input v-model="dataForm.homepageShow" :disabled="disabled" placeholder="是否首页展现，1是，2否，"></el-input>&ndash;&gt;-->
<!--        <el-radio-group v-model="dataForm.homepageShow">-->
<!--          <el-radio :label=1>是</el-radio>-->
<!--          <el-radio :label=2>否</el-radio>-->
<!--        </el-radio-group>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="首页头部展现，填写数字越大展现越靠前" prop="homepageTop">-->
<!--        <el-input v-model="dataForm.homepageTop" :disabled="disabled" placeholder="首页头部展现，填写数字越大展现越靠前"></el-input>-->
<!--      </el-form-item>-->
      <el-form-item label="小图" prop="icon">
        <el-img v-model="dataForm.icon" :disabled="disabled">
        </el-img>
        <el-input v-model="dataForm.icon" :disabled="disabled" placeholder="小图"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        disabled: false,
        visible: false,
        categoryList: [],
        goodsList: [],
        dataForm: {
          id: 0,
          sort: '',
          title: '',
          categoryId: '',
          goodsId: '',
          status: '',
          link: '',
          cover: '',
          remarks: '',
          homepageShow: '',
          homepageTop: '',
          icon: ''},
        dataRule: {
          name: [
            {required: true, message: '名称不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id, disabled) {
        this.disabled = disabled
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/mall/module/info/${this.dataForm.id}`,
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.module
              }
            })
          }
        })
        this.getCategoryList()
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              this.$http({
                url: `/mall/module/${!this.dataForm.id ? 'save' : 'update'}`,
                method: 'post',
                data: this.dataForm
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500
                  })
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            }
          })
      },
      getCategoryList () {
        this.$http({
          url: `/mall/modulecategory/queryAll`,
          method: 'get',
          params: {
            'status': 1
          },
          data: this.dataForm
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.categoryList = data.list
          }
        })
      },
      remoteMethod (query) {
        if (query !== '') {
          this.$http({
            url: `/mall/goods/queryAll`,
            method: 'get',
            params: {
              'name': query,
              'isDelete': 1
            },
            data: this.dataForm
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.goodsList = data.list
            }
          })
        }
      }
    }
  }
</script>
